#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QTextStream>
#include "fem/trianglenet.h"
#include "linal/vector.h"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QStringList paths;
    QStringList materials;

    QTextStream cout(stdout);
    cout << "test";

    paths << QCoreApplication::trUtf8("e:/Антонов Павел/Результаты/BaF2/Cylindrical/Conduction[dT40][gradT12][V2.8]/output/");
    paths << QCoreApplication::trUtf8("e:/Антонов Павел/Результаты/BaF2/Cylindrical/Convection[dT40][gradT12][V2.8]/output/");
    paths << QCoreApplication::trUtf8("e:/Антонов Павел/Результаты/Si/Cylindrical/Conduction[dT40][gradT12][V2.8]/output/");
    paths << QCoreApplication::trUtf8("e:/Антонов Павел/Результаты/Si/Cylindrical/Convection[dT40][gradT12][V2.8]/output/");

    materials << "BaF2" << "BaF2" << "Si" << "Si";

    for(int j = 0; j < paths.size(); ++j)
    {
        QString path = paths[j];
        QFile mass_file(path + "txt/mass.dat");
        QFile volume_file(path + "txt/volume.dat");
        QTextStream ostream_mass(&mass_file);
        QTextStream ostream_volume(&volume_file);

        mass_file.open(QFile::WriteOnly | QFile::Text);
        volume_file.open(QFile::WriteOnly | QFile::Text);
        foreach (QString filename, QDir(path).entryList(QStringList("*T.dat"), QDir::Files | QDir::CaseSensitive, QDir::Name))
        {
            fem::TriangleNet net(path + filename.left(filename.size() - 5) + "rz.dat",
                                 path + filename.left(filename.size() - 5) + "nvtr.dat");

            double mass = 0.;
            double volume = 0.;
            for(int i = 0; i < net.numOfElements(); ++i)
            {
                if(net.material(i) == 2)
                {
                    double detD;
                    detD = (net.point(i, 1).x() - net.point(i, 0).x())*(net.point(i, 2).y() - net.point(i, 0).y()) -
                           (net.point(i, 2).x() - net.point(i, 0).x())*(net.point(i, 1).y() - net.point(i, 0).y());

                    volume += 2*3.14*(net.point(i, 0).x() + net.point(i, 1).x() + net.point(i, 2).x())*qAbs(detD)/6.;
                }
            }

            double density;
            if(materials[j] == "BaF2")
                density = 4830.;
            if(materials[j] == "Si")
                density = 2550.;

            ostream_mass << filename.left(5).toInt()*1.5/60. << " " << density*volume << endl;
            ostream_volume << filename.left(5).toInt()*1.5/60. << " " << volume << endl;
        }
        mass_file.close();
        volume_file.close();
    }
}
